﻿<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Threading" %>



<script runat="server">
  private void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack || (string)Session["CultureName"] != CultureInfo.CurrentCulture.Name)
    {

      // Populate the language dropdown with the list of available languages on the server
      string[] ResLangs = { "en", "it", "fr", "nl" };
      DropDownLanguage.Items.Clear();   // Clears the dropdown in case of a culture change
      foreach (string ResLanguage in ResLangs)
      {
        CultureInfo TempCultureInfo = new CultureInfo(ResLanguage);
        ListItem ResourceLanguage = new ListItem(TempCultureInfo.NativeName, TempCultureInfo.Name);
        if (TempCultureInfo.Equals(CultureInfo.CurrentUICulture))
        {
          ResourceLanguage.Selected = true;
        }
        DropDownLanguage.Items.Add(ResourceLanguage);

      }
      Session["CultureName"] = CultureInfo.CurrentCulture.Name;
      Session["ShortCultureName"] = ((String)Session["CultureName"]).Substring(0, 2);
      Session["IdentityName"] = HttpContext.Current.User.Identity.Name;
    }
    this.Page_Load_from_template(sender, e);
  }
  protected override void InitializeCulture()
  {
    // override virtual method InitializeCulture() to check if profile contains a user language setting
    string UserCulture = Profile.GetPropertyValue("PreferredCulture").ToString();
    if (UserCulture != "")
    {
      // there is a user language setting in the profile: switch to it
      Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserCulture);
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture);
    }
  }

  

  protected void DropDownLanguage_SelectedIndexChanged(object sender, EventArgs e)
  {
    string SelectedLanguage = DropDownLanguage.SelectedValue.ToString();
    //Save selected user language in profile 
    Profile.SetPropertyValue("PreferredCulture", SelectedLanguage);

    //Force re-initialization of the page to fire InitializeCulture()
    Response.Redirect(Request.Url.LocalPath);
  }

</script>

<asp:content id="contentMultilingual" contentplaceholderid="cpLanguage" runat="server">

  <asp:DropDownList ID="DropDownLanguage" runat="server" OnSelectedIndexChanged="DropDownLanguage_SelectedIndexChanged"
    AutoPostBack="True">
  </asp:DropDownList>

</asp:content>